package com.dys.requestdomain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @Author: 疾风
 * @ClassName: Servlet01
 * @Description: Request请求域
 * @CreateDate: 2024/09/23
 * @Version: v1.0.0
 */
@WebServlet(urlPatterns = "/addToRequest.do")
public class Servlet01 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行Servlet01的service方法......");
        // 向请求域中添加数据
        // http://localhost:8080/servlet_request_domain10_Web_exploded/addToRequest.do?username=zhangsan&password=123456
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "A", "B", "C", "D", "E", "F", "G", "H");
        req.setAttribute("list", list);
        // 后面的数据会覆盖前面的数据
        req.setAttribute("sex", "男");
        req.setAttribute("sex", "女");
        req.setAttribute("name", "百里寒");

        // 请求转发
        req.getRequestDispatcher("/readFromRequest.do").forward(req, resp);

        // 重定向
        // resp.sendRedirect("readFromRequest.do");
        // 会产生新的Request对象
        // resp.sendRedirect(this.getServletContext().getContextPath() +"/readFromRequest.do");
    }
}
